package com.idbk.chargestation.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.idbk.chargestation.R;
import com.idbk.chargestation.activity.user.ActivityMyWallet;
import com.idbk.chargestation.adapter.MywalletAdapter;
import com.idbk.chargestation.api.ChargeStationAPI;
import com.idbk.chargestation.base.BaseActivity;
import com.idbk.chargestation.bean.JsonMywalletRecord;
import com.idbk.chargestation.bean.JsonMywalletRecordList;
import com.idbk.chargestation.net.EHttpResponse;
import com.idbk.chargestation.util.GsonUtils;
import com.idbk.chargestation.util.UIUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 我的钱包 里面的 3个fragment
 * @author liangzw,lupc,zhx
 *
 */
public class FragmentMyWallet extends Fragment implements OnClickListener{

	protected static final String TAG = "FragmentWalletRecharge";

	public final static int PAGE_SIZE = 10;
	
	private int mRecordType;
	
	private Context mContext;
	
	private View mBaseView;

	private List<JsonMywalletRecord> mData;//声明集合
	private MywalletAdapter mAdapter;//声明适配器
	private PullToRefreshListView mListView;
	private LinearLayout mLayoutEmpty, mLayoutError;
	private int mPageIndex;//页索引，每页数据量，类型

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		mContext = getActivity();

		mBaseView = inflater.inflate(R.layout.fragment_listview, container, false);

		setupView();

		return mBaseView;
	}

	private void setupView() {
		mRecordType = getArguments().getInt(ActivityMyWallet.RECORD_TYPE, JsonMywalletRecord.RECORD_TYPE_ALL);
		mLayoutEmpty= (LinearLayout) mBaseView.findViewById(R.id.tj_empty_view);
		mLayoutError = (LinearLayout) mBaseView.findViewById(R.id.tj_error_view);
		mLayoutError.setOnClickListener(this);
		
		mListView = (PullToRefreshListView) mBaseView.findViewById(R.id.pullToRefreshListView);
		mData = new ArrayList<JsonMywalletRecord>();
		mListView.setMode(Mode.BOTH);
		mListView.setOnRefreshListener(new OnRefreshListener2<ListView>() {
			@Override
			public void onPullDownToRefresh(
					PullToRefreshBase<ListView> refreshView) {
				//下拉重置数据
				ChargeStationAPI.getWalletRecord(
						1, 
						PAGE_SIZE, 
						mRecordType, 
						mResponse);
			}

			@Override
			public void onPullUpToRefresh(
					PullToRefreshBase<ListView> refreshView) {
				//上拉加载数据
				ChargeStationAPI.getWalletRecord(
						mPageIndex + 1, 
						PAGE_SIZE, 
						mRecordType, 
						mLoadMoreResponse);
			}
		});

		mAdapter = new MywalletAdapter(mContext,mData,mListView);
		mListView.setAdapter(mAdapter);
	}

	//第一次请求数据
	private void setupData(){
		//向服务器请求数据
		ChargeStationAPI.getWalletRecord(
				1, 
				PAGE_SIZE, 
				mRecordType,
				mResponse);
	}
	
	@Override
	public void onStart() {
		setupData();			
		super.onStart();
	}		

	private final EHttpResponse mResponse = new EHttpResponse() {

		@Override
		public void onEResponse(String response) {
			JsonMywalletRecordList result = GsonUtils.toBean(JsonMywalletRecordList.class, response);
			if (BaseActivity.handleResponseStatus(getActivity(), result)) {
				if (result.allRecords == null || result.allRecords.size() <= 0) {
					UIUtil.showEmpty(mLayoutEmpty, mLayoutError);
					mListView.setVisibility(View.GONE);
					
				} else {
					UIUtil.hideAll(mLayoutEmpty, mLayoutError);
					mListView.setVisibility(View.VISIBLE);
					mPageIndex = 1;
					mData.clear();
					mData.addAll(result.allRecords);//添加集合
					mAdapter.notifyDataSetChanged();
				}
			}
		}		

		@Override
		public void onEErrorResponse(VolleyError error) {
			UIUtil.showNetError(mLayoutEmpty, mLayoutError);
			mListView.setVisibility(View.GONE);
		}

		@Override
		public void onEFinish() {
			// TODO Auto-generated method stub
			super.onEFinish();
			mListView.onRefreshComplete();
		}	
	};
	

	private final EHttpResponse mLoadMoreResponse = new EHttpResponse() {

		@Override
		public void onEResponse(String response) {
			JsonMywalletRecordList result = GsonUtils.toBean(JsonMywalletRecordList.class, response);
			if (BaseActivity.handleResponseStatus(getActivity(), result)) {
				if(result.allRecords == null || result.allRecords.size() < 1){
					Toast.makeText(mContext, "没有更多数据了", Toast.LENGTH_SHORT).show();
					
				} else {					
					mPageIndex += 1;
					mData.addAll(result.allRecords);//添加集合
					mAdapter.notifyDataSetChanged();
				}
			}
		}		

		@Override
		public void onEErrorResponse(VolleyError error) {
			UIUtil.showNetError(mLayoutEmpty, mLayoutError);
			mListView.setVisibility(View.GONE);
		}

		@Override
		public void onEFinish() {
			// TODO Auto-generated method stub
			super.onEFinish();
			mListView.onRefreshComplete();
		}	
	};

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.tj_error_view:
			setupData();
			break;
		default:
			break;
		}
	}
}
